<template>
  <div class="w-full">
    <div class="mb-2">
      <el-tag>{{ $t("app.actionNew") }}</el-tag>
    </div>

    <el-table :data="items" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column :label="$t('app.name')" prop="name"></el-table-column>
      <el-table-column :label="$t('app.state')" prop="state">
        <template slot-scope="scope">
          <!-- 在线/离线 -->
          <el-tag :type="stateColor[scope.row.state]">{{
            $t("app." + scope.row.state)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('app.oper')" width="180px">
        <template slot-scope="scope">
          <!-- 刷新状态 -->
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            size="mini"
            @click="handleRefreshApp(scope.row['_id'])"
          ></el-button>
          <!-- 删除 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleDeletApp(scope.row['_id'])"
            v-if="isAdmin"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-pagination
      layout="total, prev, pager, next"
      background
      :page-size="pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      style="text-align: center; margin-top: 20px"
    >
    </el-pagination>
  </div>
</template>


<script>
import { mapGetters } from "vuex";
import { getApps } from "@/api/app";

export default {
  computed: {
    ...mapGetters(["isAdmin"]),
  },
  props: ["index", "keyword"],
  data() {
    return {
      page: 1,
      pageSize: 10,
      items: [],
      total: 0,

      stateColor: {
        active: "success",
        inactive: "info",
      },
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      getApps({
        type: this.index,
        keyword: this.keyword,
        page: this.page,
        pageSize: this.pageSize,
      }).then((res) => {
        this.total = res.data.total;
        this.items = res.data.list;
      });
    },
    handleCurrentChange(newPage) {
      this.page = newPage;
      this.fetchData();
    },
    handleRefreshApp(id) {
      console.log(id);
    },
    handleDeletApp(id) {
      console.log(id);
    },
    handleUpdateApp(item) {
      console.log(item);
    },
  },
};
</script>
